package com.region.integral.common_user.obtain.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.common_user.obtain.dao.IIntegralReplyComment;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.common_user.pojo.CUIEntity;
import com.region.integral.common_user.xml.Regulation;
import com.region.integral.common_user.xml.dto.CommentGoods;
import com.region.integral.common_user.xml.dto.CommentWeiBo;
import com.region.integral.common_user.xml.dto.ReplyComment;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.XMLRegulationUtil;
@Component("iReplyComment")
public class IntegralReplyCommentImpl extends BaseHibernate implements IIntegralReplyComment {

	@Override
	public void replyComment(int userid,IntegralData integralData)throws IntegralException {
		CUIEntity cuiEntity = getCUIEntity(userid);
		if (cuiEntity != null) {
			int replyCommentNum = cuiEntity.getReplyCommentNum();
			
			Regulation regulation = XMLRegulationUtil.unmarshaller();
			List<ReplyComment> replyComments = regulation.getReplycomment();
			for (ReplyComment replyComment : replyComments) {
				if (replyCommentNum != 0 && replyCommentNum % replyComment.getRate() == 0) {
					int currIntegral = cuiEntity.getCurrIntegral();
					int maxIntegral = cuiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ replyComment.getIntegral();
					if (maxIntegral < totalIntegral) {
						cuiEntity.setMaxIntegral(totalIntegral);
					}
					cuiEntity.setCurrIntegral(totalIntegral);
					integralData.setSuccee(true);
					integralData.setObtainIntegral(replyComment.getIntegral());
				}
			}
			cuiEntity.setReplyCommentNum(replyCommentNum + 1);
			hibernateTemplate.update(cuiEntity);
		} 
	}

}
